<template>
  <div class="common-layout">
    <el-container class="login-container">
      <!--头部区域-->
      <el-header class="login-header">
        <div>
          <el-icon style="margin-left: 10px">
            <MoonNight />
          </el-icon>
          <span> 东软颐养中心 </span>
        </div>
      </el-header>
      <el-main class="login-main">
        <div class="login-main-left">
          <img src="../assets/loginPic.jpg" alt="picture" />
        </div>
        <div class="login-main-right">
          <el-card class="login-main-right-card">
            <el-form class="login-main-right-card-form" ref="loginFormRef" :model="loginForm" :rules="loginFormRules">
              <div class="login-main-right-card-form-header">欢迎使用</div>
              <!--用户名-->
              <el-form-item prop="username">
                <el-input v-model="loginForm.username" :prefix-icon="User" placeholder="请输入用户名"></el-input>
              </el-form-item>
              <!--密码-->
              <el-form-item prop="password">
                <el-input v-model="loginForm.password" :prefix-icon="Lock" type="password" show-password
                  placeholder="请输入密码"></el-input>
              </el-form-item>
              <!--按钮区-->
              <el-form-item class="btns">
                <el-button color="#578BBA" @click="submitLogin" style="width: 100%; color: #ffffff">登录
                </el-button>
              </el-form-item>
              <!-- 去注册按钮或方式 -->
              <div class="register-link">
                还没有账号？
                <router-link to="/register">去注册</router-link>
              </div>
            </el-form>
          </el-card>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

// 导入封装的接口方法
import * as user from "@/api/account.js";
import account from "../stores/account";

const router = useRouter();

const store = useStore();

//表单引用
const loginFormRef = ref();

//表单数据的绑定对象
const loginForm = reactive({
  username: "",
  password: "",
});

//表单校验对象
const loginFormRules = reactive({
  username: [
    { required: true, message: "用户名不能为空", trigger: "blur" },
    { min: 1, max: 10, message: "用户名长度1~10个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "密码不能为空", trigger: "blur" },
    { min: 3, max: 10, message: "密码长度3~10个字符", trigger: "blur" },
  ],
});

const submitLogin = () => {
  // 调用接口方法
  loginFormRef.value.validate((valid) => {
    if (valid) {
      // const url = '/api/user/login'
      user
        .loginAPI(loginForm)
        .then((response) => {
          //登录失败
          if (response.data.code !== 2000) {
            ElMessage.error(response.data.message);
            return;
          }
          //登录成功
          ElMessage({
            message: response.data.message,
            type: "success",
          });
          const account = response.data.data[0];
          store.commit("saveAccount", account);
          if (account.accType == 0) {
            router.push({ path: "/administerhome" });
          } else if (account.accType == 1) {
            router.push({ path: "/nursinghome" });
          } else if (account.accType == 2) {
            router.push({ path: "/userhome" });
          }
        })
      // .finally(() => {

      //   // openSocket("ws://localhost:8081/?userId=" + store.getters.getId);
      // });
    }
  });
};
</script>

<style lang="less" scoped>
.login-container {
  height: 100vh;
}

.login-header {
  background-color: #578bba;
  display: flex;
  justify-content: space-between;
  padding-left: 0%;
  align-items: center;
  color: #ffffff;
  font-size: 20px;
  letter-spacing: 3px;
  font-size: 30px;
  font-weight: bold;

  >div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }

    img {
      width: 50px;
      height: 50px;
      margin-left: 20px;
    }
  }
}

.login-main {
  background-color: #e9e9e9;
  display: flex;
  justify-content: space-evenly;
}

.login-main-left {
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-main-right {
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-main-right-card {
  width: 350px;
  height: 60%;
  display: flex;
  flex-direction: column;
}

.login-main-right-card-form-header {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 30px;
  letter-spacing: 8px;
  margin-bottom: 20px;
  margin-top: 20px;
  color: #578bba;
}

.login-main-right-card-form {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.el-form-item {
  margin-top: 20px;
}

.el-input {
  width: 100%;
}

.btns {
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-link {
  margin-left: auto;
  font-size: 14px;
}

.register-link a {
  color: blue;
  text-decoration: underline;
  transition: color 0.3s, text-decoration 0.3s;
}

.register-link a:hover {
  color: #50bcb6;
  text-decoration: none;
}
</style>
